<?php

class Logger_Mongo extends Logger
{

	/**
	 * main logger connection
	 * @var string
	 */

	protected $_connection = null;

	/**
	 * @var array
	 */
	protected $_data = array();

	/**
	 * create from parent parent constructors
	 */
	protected function __construct($name, $config)
	{
		/**
		 * call parent constructor
		 */
		parent::__construct($name, $config);

		/**
		 * check correct file name
		 */
		$this -> _connection = $this -> getConfig('connection', null);

		/**
		 * do not add regisiter shutdown function, it sometime may touch
		 */
	}

	/**
	 * @param string $level
	 * @param string $message
	 * @return Logger_Mongo
	 */
	public function write($level, $message)
	{
		$this -> _data[] = array(
			'level' => $level,
			'message' => $this -> format($level, $message),
			'created' => new MongoDate()
		);
		return $this;
	}

	/**
	 * flush log data to database.
	 */
	public function flush()
	{
		if ($this -> _data)
		{
			DbNosql::inst($this -> _connection) -> batchInsert('logs', $this -> _data);
		}
		$this->_data=  array();
	}

}
